package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcDeviceOnlineRecord;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.deviceOnlineRecord.DeviceOnlineQueryDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcDeviceOnlineRecordService;
import com.csun.cmny.provider.validator.DmcDeviceAddValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/online", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcDeviceOnlineRecordController extends BaseController {

    @Resource
    private DmcDeviceOnlineRecordService dmcDeviceOnlineRecordService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询设备在离线记录列表 - 分页查询")
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody DeviceOnlineQueryDto deviceOnlineQueryDto) {

        //deviceOnlineQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        List<DmcDeviceOnlineRecord> deviceOnlineRecords=dmcDeviceOnlineRecordService.queryListWithPage(deviceOnlineQueryDto);
        PageInfo pageInfo = new PageInfo<>(deviceOnlineRecords);


        // return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "保存设备在离线记录")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Wrapper save(@RequestBody DmcDeviceOnlineRecord dmcDeviceOnlineRecord) {

        dmcDeviceOnlineRecord.setGroupId(getLoginAuthDto().getGroupId());
        Integer result = dmcDeviceOnlineRecordService.save(dmcDeviceOnlineRecord);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
}
